<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-2
 * Time: 上午9:20
 * @author 郑钟良<zzl@ourstu.com>
 */

namespace Appstore\Model;


use Think\Model;

class AppstoreCodeModel extends Model{
    /**
     * 管理员后台生成云码
     * @param array $data
     * @param int $num
     * @param int $length
     * @return mixed
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function createCode($data = array(), $num = 1,$length=11)
    {
        $data['uid']=get_uid();
        $data['create_time']=time();
        $data['status']=1;
        $dataList = array();
        do {
            $dataList[] = $this->createOneCode($data, $length);
        } while (count($dataList) < $num);
        $res = $this->addAll($dataList);
        if ($res) {
            $result['status'] = 1;
            $result['url'] = U('Admin/Appstore/code', array('status' => 1));
        } else {
            $result['status'] = 0;
            $result['info'] = "生成云码时失败！" . $this->getError();
        }
        return $result;
    }

    public function getCode($map)
    {
        $code=$this->where($map)->find();
        if($code){
            $type=D('Appstore/AppstoreCodeType')->getData($code['type_id']);
            if($type&&$type['status']==1){
                $code['version_ids']=$type['version_ids'];
            }else{
                $code=null;
            }
        }
        return $code;
    }

    /**
     * 设置云码被使用状态
     * @param $map
     * @param int $status
     * @return bool
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function setStatus($map,$status=2)
    {
        $res=$this->where($map)->setField('status',$status);
        return $res;
    }

    /**
     * 获取分页云码列表
     * @param array $map
     * @param int $page
     * @param int $r
     * @param string $order
     * @return array|null
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function getList($map = array(), $page = 1, $r = 20, $order = 'id desc')
    {
        $totalCount = $this->where($map)->count();
        if ($totalCount) {
            $dataList = $this->where($map)->page($page, $r)->order($order)->select();
            return array($this->_initSelectData($dataList), $totalCount);
        }
        return array(null, 0);
    }

    /**
     * 获取云码列表
     * @param array $map
     * @param string $order
     * @return array|null
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function getListAll($map = array(), $order = 'id desc')
    {
        $dataList = $this->where($map)->order($order)->select();
        return $this->_initSelectData($dataList);
    }

    /**
     * 初始化查询信息
     * @param array $dataList
     * @return array
     * @author 郑钟良<zzl@ourstu.com>
     */
    private function _initSelectData($dataList = array())
    {
        $versionModel=D('Appstore/AppstoreVersion');
        $goodsModel=D('Appstore/AppstoreGoods');
        $codeTypeModel=D('Appstore/AppstoreCodeType');
        $type=array(
            1=>'插件',
            2=>'模块',
            3=>'主题',
            4=>'服务'
        );
        foreach ($dataList as &$val) {
            $codeType=$codeTypeModel->getData($val['type_id']);
            $codeType['version_ids']=explode(',',$codeType['version_ids']);
            foreach($codeType['version_ids'] as $vl){
                $version=$versionModel->where(array('id'=>$vl))->find();
                $version['goods']=$goodsModel->where(array('id'=>$version['goods_id']))->find();
                $val['versions_list'][]=$version;
                $val['versions'][]=$type[$version['goods']['entity']].'>'.$version['goods']['title'].'【'.$version['title'].'】';
            }
            $val['versions']=implode('；',$val['versions']);
            $val['user']='['.$val['uid'].']'.get_nickname($val['uid']);
            $val['title']=$codeType['title'];
            $val['codeType']=$codeType;
        }
        unset($val);
        return $dataList;
    }

    /**
     * 创建云码
     * @param array $data
     * @param $length
     * @return array|mixed
     * @author 郑钟良<zzl@ourstu.com>
     */
    private function createOneCode($data = array(), $length)
    {
        $length = $length ? $length : 11;
        do {
            //生成随机数
            $map['code'] = create_rand($length);
        } while ($this->where($map)->count());
        $data['code'] = $map['code'];
        $data = $this->create($data);
        return $data;
    }
} 